<template>
  <div id="supplierInform">
    <el-row
      type="flex"
      justify="space-between"
      align="center"
      :gutter="20"
      style="width: 100%; margin-bottom: 10px"
    >
      <el-col
        :span="10"
        style="display: flex"
        ><span style="width: 60px; height: 40px; line-height: 40px">搜索：</span
        ><el-input v-model="projectInput"></el-input
      ></el-col>
      <el-col
        :span="7"
        style="display: flex"
        ><el-button
          type="primary"
          @click="searchHandler"
          >查询</el-button
        ><el-button
          plain
          @click="resetHandler"
          >重置</el-button
        ></el-col
      >
      <el-col
        :span="7"
        style="display: flex"
      ></el-col>
    </el-row>
    <el-row
      type="flex"
      justify="center"
      align="center"
      style="width: 100%; margin-bottom: 10px"
    >
      <el-col
        :span="2"
        style="display: flex"
        ><el-button
          type="primary"
          @click="addHandler"
          >新增</el-button
        >
        <el-button
          type="primary"
          @click="shuaxin"
          >刷新</el-button
        ></el-col
      >
      <el-col
        :span="20"
        style="display: flex"
      ></el-col>
      <el-col
        :span="2"
        :pull="1"
        style="display: flex"
      ></el-col>
    </el-row>
    <el-row>
      <el-table
        :data="renderData"
        border
        style="width: 100%"
        :header-cell-style="{ background: '#606266', color: '#fff' }"
        height="800"
      >
        <el-table-column
          type="index"
          label="序号"
          width="100"
          align="center"
        />
        <el-table-column
          prop="bdmc"
          label="项目ID"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="fzr"
          label="项目名称"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="lxdh"
          label="工程类型"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="cz"
          label="建设单位"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="dzyh"
          label="项目编号"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="bz"
          label="项目类型"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="180"
          align="center"
        >
          <template
            style="width: 110px"
            slot-scope="scope"
          >
            <el-button
              type="text"
              @click="editHandler(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              @click="editHandler(scope.row)"
              >查看</el-button
            >
            <el-button
              type="text"
              style="color: red"
              @click="deleteHandler(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="position: absolute; right: 50px">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-row>
    <!-- 编辑 -->
    <el-dialog
      :title="editTiele"
      :visible.sync="editDialogStatus"
      width="30%"
      :modal-append-to-body="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        :model="from"
        ref="from"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-row class="mgb-10 from">
          <el-form-item
            label="项目ID:"
            prop="monthPlanId"
          >
            <el-input
              placeholder="项目ID"
              v-model="from.bdmc"
            >
            </el-input>
          </el-form-item>
        </el-row>
        <el-row class="mgb-10 from">
          <el-form-item
            label="项目名称:"
            prop="monthPlanId"
          >
            <el-input
              placeholder="项目名称"
              v-model="from.gysmc"
            >
            </el-input>
          </el-form-item>
        </el-row>
        <el-row class="mgb-10 from">
          <el-form-item
            label="工程类型:"
            prop="monthPlanId"
          >
            <el-select
              style="width: 102%"
              v-model="from.gclx"
              placeholder="入库类型"
            >
              <el-option
                label="公路"
                value="公路"
              >
              </el-option>
              <el-option
                label="水路"
                value="水路"
              >
              </el-option>
              <el-option
                label="建筑"
                value="建筑"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="mgb-10 from">
          <el-form-item
            label="建设单位:"
            prop="monthPlanId"
          >
            <el-select
              style="width: 102%"
              v-model="from.gclx"
              placeholder="入库类型"
            >
              <el-option
                label="公司单位一"
                value="公司单位一"
              >
              </el-option>
              <el-option
                label="公司单位二"
                value="公司单位二"
              >
              </el-option>
              <el-option
                label="公司单位三"
                value="公司单位三"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="mgb-10 from">
          <el-form-item
            label="项目编号:"
            prop="monthPlanId"
          >
            <el-input
              placeholder="请输入"
              v-model="from.cz"
            >
            </el-input>
          </el-form-item>
        </el-row>
        <el-row class="mgb-10 from">
          <el-form-item
            label="项目类型:"
            prop="monthPlanId"
          >
            <el-select
              style="width: 102%"
              v-model="from.gclx"
              placeholder="入库类型"
            >
              <el-option
                label="新增"
                value="新增"
              >
              </el-option>
              <el-option
                label="扩建"
                value="扩建"
              >
              </el-option>
              <el-option
                label="改建"
                value="改建"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="editDialogStatus = false">取 消</el-button>
        <el-button
          type="primary"
          @click="editSaveHandler"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import {
//   getProjectList,
//   projectDetail,
//   addProject,
//   updateProject,
//   deleteProject,
// } from "../../../http/projectMg.js";
export default {
  data() {
    return {
      dialogVisible: false,
      projectInput: '',
      renderData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      isSearch: false,
      // edit
      editTiele: '',
      editDialogStatus: false,
      editProjectIdentifyInput: '',
      editProjectInput: '',
      from: {},
    }
  },

  mounted() {
    // this.getProjectListApi();

    for (let i = 1; i < 5; i++) {
      this.renderData.push({
        bdmc: '231101000' + i,
        fzr: '某某某示例项目',
        lxdh: '公路',
        cz: '广东省路桥建设发展有限公司路达分公司',
        dzyh: 'XM20231101000' + i,
        bz: '新增',
      })
    }
  },

  methods: {
    // 搜索
    searchHandler() {
      this.isSearch = true
      if (this.projectInput === '') {
        this.$message({
          message: '请输入查询条件',
          type: 'warning',
        })
      } else {
        // this.getProjectListApi(1, this.pageSize, this.projectInput);
      }
    },
    // 重置
    resetHandler() {
      this.getProjectListApi()
      this.projectInput = ''
      this.isSearch = false
    },
    shuaxin() {
      // this.getProjectListApi();
      // this.projectInput = "";
      // this.isSearch = false;
    },
    // 新增
    addHandler() {
      this.editTiele = '新增'
      this.editDialogStatus = true
      this.editProjectIdentifyInput = ''
      this.editProjectInput = ''
    },
    //   编辑
    editHandler(v) {
      this.editTiele = '编辑'
      this.editDialogStatus = true
      this.from = v
    },
    // 删除
    deleteHandler(v) {
      console.log('delete::: ', v)
      this.$confirm('此操作将永久删除该信息?', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          //   this.deleteProjectApi([v.id]);
          this.renderData = this.renderData.filter((ele) => {
            var flag = false
            // 如果不一致，则保留该行
            for (const key in v) {
              if (ele[key] != v[key]) {
                flag = true
                break
              }
            }
            return flag
          })
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 每页几条
    handleSizeChange(v) {
      this.pageSize = v
      this.currentPage = 1
      if (this.isSearch === true) {
        // this.getProjectListApi(1, v, this.projectInput);
      } else {
        // this.getProjectListApi(1, v);
      }
    },
    // 分页
    handleCurrentChange(v) {
      this.currentPage = v
      if (this.isSearch === true) {
        // this.getProjectListApi(v, this.pageSize, this.projectInput);
      } else {
        // this.getProjectListApi(v, this.pageSize);
      }
    },
    // editDialog保存
    editSaveHandler() {
      this.editDialogStatus = false
      let params = {
        mark: this.editProjectIdentifyInput,
        name: this.editProjectInput,
      }

      if (this.editTiele == '新增') {
        this.addProjectApi(params)
      } else {
        this.updateProjectApi(params)
      }
    },

    /**接口 */
    // 查询项目列表
    getProjectListApi(pageNum = 1, pageSize = 10, name = '') {
      let params = {
        pageNum,
        pageSize,
        name,
      }

      console.log(params)

      //   getProjectList(params).then((res) => {
      //     if (res.code == 200) {
      //       this.renderData = res.data.records;
      //       this.total = res.data.total;
      //       this.pageSize = res.data.size;
      //     }
      //   });
    },
    // 项目详情
    projectDetailApi(params) {
      console.log(params)
      //   projectDetail(params).then((res) => {
      //     console.log("res::: ", res);
      //   });
    },
    // 新增项目
    addProjectApi(params) {
      console.log(params)

      this.renderData.push({
        bdmc: '231101000',
        fzr: '某某某示例项目',
        lxdh: '公路',
        cz: '广东省路桥建设发展有限公司路达分公司',
        dzyh: 'XM20231101000',
        bz: '新增',
      })

      //   addProject(params).then((res) => {
      //     if (res.code == 200) {
      //       let pgn =
      //         Number.isInteger(this.total / this.pageSize) == true
      //           ? this.total / this.pageSize + 1
      //           : Math.ceil(this.total / this.pageSize);
      //       this.getProjectListApi(pgn, this.pageSize);
      //       this.$message.success("新增成功");
      //     } else {
      //       this.$message.error("新增失败");
      //     }
      //   });
    },
    // 编辑项目
    updateProjectApi(params) {
      console.log(params)
      //   updateProject(params).then((res) => {
      //     if (res.code == 200) {
      //       this.getProjectListApi(this.currentPage, this.pageSize);
      //       this.$message.success("编辑成功");
      //     } else {
      //       this.$message.error(res.message);
      //     }
      //   });
    },
    // 删除项目
    deleteProjectApi(idList = '') {
      let params = { idList }
      console.log(params)
      //   deleteProject(params).then((res) => {
      //     let pgn =
      //       Number.isInteger(this.total / this.pageSize) == true
      //         ? this.total / this.pageSize + 1
      //         : Math.ceil(this.total / this.pageSize);
      //     if (res.code == 200 && this.search === true) {
      //       this.getProjectListApi(pgn, this.pageSize, this.projectInput);
      //       this.$message.success("删除成功");
      //     } else if (res.code == 200 && this.search === false) {
      //       this.getProjectListApi(pgn, this.pageSize);
      //       this.$message.success("删除成功");
      //     } else {
      //       this.$message.error(res.message);
      //     }
      //   });
    },
  },
}
</script>

<style lang="less" scoped>
#supplierInform {
  width: 95%;
  height: 100%;
  margin: 0 auto;
  margin-top: 1rem;
  .from {
    .el-input-group__prepend {
      width: 110px;
    }
  }
}
.el-date-editor {
  margin: 0 10px;
}
.imgBox {
  position: relative;
  top: 0;
  left: 0;
  .uploadImg {
    position: absolute;
    top: 30%;
    left: 30%;
    visibility: hidden;
  }
}
.imgBox:hover .uploadImg {
  visibility: visible;
}
:deep(.el-dialog__header) {
  background-color: rgb(43, 43, 53);
}
:deep(.el-dialog__title) {
  color: #fff;
}
// 图片上传
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.pDiv {
  background-color: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-right: 0px;
  border-radius: 4px 0 0 4px;
  text-align: center;
  height: 40px;
  line-height: 40px;
  width: 13.7%;
}
:deep(.pSelect) {
  .el-input__inner {
    border-radius: 0 4px 4px 0;
    width: 107.5%;
  }
  .el-input__suffix {
    right: -5%;
  }
}
.mgb-10 {
  margin-bottom: 5px;
}
</style>
<style lang="less">
#supplierInform {
  .from {
    .el-input-group__prepend {
      width: 120px;
    }
    .el-select {
      width: 500px;
    }
  }
}
</style>
